import Mock from 'mockjs'

import { updateList, increment } from '../mutations-type'

export default {
  namespaced: true,
  state: {
    list: []
  },
  getters: {
    filterList (state: { list: { map: (arg0: (item: any) => any) => void; }; }, getters: any) {
      return state.list.map(item => {
        item.sexName = item.sex === 1 ? '男' : '女'
        return item
      })
    }
  },
  mutations: {
    [updateList] (state: { list: any; }, payload: any) {
      state.list = payload
    }
  },
  actions: {
    getList ({ commit }: any, payload: any) {
      return new Promise(resolve => {
        setTimeout(() => {
          const { data: list } = Mock.mock({
            [`data|${payload || '3-10'}`]: [{
              'id|+1': 1,
              name: '@cname()',
              'sex|1': [1, 0]
            }]
          })
          commit(updateList, list)
          commit(increment, 4, { root: true }) // root表示触发根store的increment函数
          resolve()
        }, 1000)
      })
    }
  }
}
